﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BPM.Base.Tools;
using System.ServiceModel;
using ESOL.Services;

namespace BPM.Foundation.Server
{
    public partial class Index : Form
    {
        public Index()
        {
            InitializeComponent();
        }

        BPM.Foundation.PWService.WCFService pws;
        BPM.Foundation.WFService.WCFService wfs;
        BPM.RepotrService.WCFService rps;
        ESOL.Services.WCFService bpts;
        ESOL.Services.WCFService msts;
        ESOL.Services.WCFService prods;
        ESOL.Services.WCFService sales;
        ESOL.Services.WCFService syss;
        ESOL.Services.WCFService invs;
        ESOL.Services.WCFService qcs;
        ESOL.Services.WCFService fas;
        ESOL.Services.WCFService hrs;

        void timer_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now >= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)
            && DateTime.Now <= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 30, 0))
            {
                button_time_Click(null, null);
            }
        }

        private void button_Start_Click(object sender, EventArgs e)
        {
            string pwserurl = MyConfig.getAppValue("wcfpwserviceurl");
            pws = new BPM.Foundation.PWService.WCFService(new Uri(pwserurl));

            int? v = pws.open();

            if (v == null)
            {
                MyDebug.write("权限服务已启动");
            }
            else
            {
                MyDebug.write("权限服务启动失败");
                MessageBox.Show("权限服务启动失败");
                return;
            }

            //-
            string rpserurl = MyConfig.getAppValue("wcfrpserviceurl");
            rps = new BPM.RepotrService.WCFService(new Uri(rpserurl));
            int? v1 = rps.open();
            if (v1 == null)
            {
                MyDebug.write("报表服务已启动");
            }
            else
            {
                MyDebug.write("报表服务启动失败");
                MessageBox.Show("报表服务启动失败");
                return;
            }

            //-
            string wfserurl = MyConfig.getAppValue("wcfwfserviceurl");
            wfs = new BPM.Foundation.WFService.WCFService(new Uri(wfserurl));
            int? v2 = wfs.open();
            if (v2 == null)
            {
                button_Start.Enabled = false;
                button_stop.Enabled = true;
                button_refresh.Enabled = true;
                groupBox2.Enabled = true;
                MyDebug.write("工作流服务已启动");
                this.Text = "服务已启动";

            }
            else
            {
                MyDebug.write("工作流务启动失败");
                MessageBox.Show("工作流务启动失败");
            }

            //
            string bpturl = MyConfig.getAppValue("wcfbptserviceurl");
            bpts = new ESOL.Services.WCFService(typeof(BptService), new Uri(bpturl), typeof(IBptService));
            int? v3 = bpts.open();
            if (v3 == null)
            {
                MyDebug.write("BPT业务服务已启动");
            }
            else
            {
                MyDebug.write("BPT业务服务启动失败");
                MessageBox.Show("BPT业务服务启动失败");
                return;
            }

            //
            string msturl = MyConfig.getAppValue("wcfmstserviceurl");
            msts = new ESOL.Services.WCFService(typeof(MstService), new Uri(msturl), typeof(IMstService));
            int? v4 = msts.open();
            if (v4 == null)
            {
                MyDebug.write("MST业务服务已启动");
            }
            else
            {
                MyDebug.write("MST业务服务启动失败");
                MessageBox.Show("MST业务服务启动失败");
                return;
            }

            //
            string produrl = MyConfig.getAppValue("wcfprodserviceurl");
            prods = new ESOL.Services.WCFService(typeof(ProdService), new Uri(produrl), typeof(IProdService));
            int? v5 = prods.open();
            if (v5 == null)
            {
                MyDebug.write("PROD业务服务已启动");
            }
            else
            {
                MyDebug.write("PROD业务服务启动失败");
                MessageBox.Show("PROD业务服务启动失败");
                return;
            }

            //
            string salesurl = MyConfig.getAppValue("wcfsalesserviceurl");
            sales = new ESOL.Services.WCFService(typeof(SalesService), new Uri(salesurl), typeof(ISalesService));
            int? v6 = sales.open();
            if (v6 == null)
            {
                MyDebug.write("SALES业务服务已启动");
            }
            else
            {
                MyDebug.write("SALES业务服务启动失败");
                MessageBox.Show("SALES业务服务启动失败");
                return;
            }

            //
            string sysurl = MyConfig.getAppValue("wcfsysserviceurl");
            syss = new ESOL.Services.WCFService(typeof(SysService), new Uri(sysurl), typeof(ISysService));
            int? v7 = syss.open();
            if (v7 == null)
            {
                MyDebug.write("SYS业务服务已启动");
            }
            else
            {
                MyDebug.write("SYS业务服务启动失败");
                MessageBox.Show("SYS业务服务启动失败");
                return;
            }

            //仓库
            string invurl = MyConfig.getAppValue("wcfinvserviceurl");
            invs = new ESOL.Services.WCFService(typeof(InvService), new Uri(invurl), typeof(IInvService));
            int? v8 = invs.open();
            if (v8 == null)
            {
                MyDebug.write("INV业务服务已启动");
            }
            else
            {
                MyDebug.write("INV业务服务启动失败");
                MessageBox.Show("INV业务服务启动失败");
                return;
            }

            //采购
            string pururl = MyConfig.getAppValue("wcfpurserviceurl");
            invs = new ESOL.Services.WCFService(typeof(PurService), new Uri(pururl), typeof(IPurService));
            int? v9 = invs.open();
            if (v9 == null)
            {
                MyDebug.write("PUR业务服务已启动");
            }
            else
            {
                MyDebug.write("PUR业务服务启动失败");
                MessageBox.Show("PUR业务服务启动失败");
                return;
            }

            //质检
            string qcurl = MyConfig.getAppValue("wcfqcserviceurl");
            qcs = new ESOL.Services.WCFService(typeof(QcService), new Uri(qcurl), typeof(IQcService));
            int? v10 = qcs.open();
            if (v10 == null)
            {
                MyDebug.write("QC业务服务已启动");
            }
            else
            {
                MyDebug.write("QC业务服务启动失败");
                MessageBox.Show("QC业务服务启动失败");
                return;
            }

            //固定资产
            string faurl = MyConfig.getAppValue("wcffaserviceurl");
            fas = new ESOL.Services.WCFService(typeof(FaService), new Uri(faurl), typeof(IFaService));
            int? v11 = fas.open();
            if (v11 == null)
            {
                MyDebug.write("FA业务服务已启动");
            }
            else
            {
                MyDebug.write("FA业务服务启动失败");
                MessageBox.Show("FA业务服务启动失败");
                return;
            }


            //HR
            string hrurl = MyConfig.getAppValue("wcfhrserviceurl");
            hrs = new ESOL.Services.WCFService(typeof(HrService), new Uri(hrurl), typeof(IHrService));
            int? v12 = hrs.open();
            if (v12 == null)
            {
                MyDebug.write("Hr业务服务已启动");
            }
            else
            {
                MyDebug.write("Hr业务服务启动失败");
                MessageBox.Show("Hr业务服务启动失败");
                return;
            }


            button_time_Click(null, null);
            Timer timer = new Timer();
            timer.Interval = 1800000;
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            if (pws != null)
            {
                int? v = pws.close();
                if (v == null)
                {


                    MyDebug.write("权限服务已停止");
                }
            }
            if (wfs != null)
            {
                int? v = wfs.close();
                if (v == null)
                {
                    button_stop.Enabled = false;
                    button_Start.Enabled = true;
                    button_refresh.Enabled = false;
                    groupBox2.Enabled = false;
                    this.Text = "服务已停止";
                    MyDebug.write("工作流务服务已停止");
                }
            }
        }

        private void button_time_Click(object sender, EventArgs e)
        {
            int? i = wfs.wfService.refreshTransactResidualTimelimit();
            label1.Text = i.ToString();
            wfs.wfService.saveInstanceInfo("");
            wfs.wfService.childProcessRegress("");
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            this.instanceList.Items.Clear();
            foreach (var v in wfs.wfService.getWorkflowInstanceFromRuntime(""))
            {
                this.instanceList.Items.Add(v.ToString());
            }
        }
    }
}
